package com.example.caiyu.Controller;

import com.example.caiyu.Pojo.Commodity;
import com.example.caiyu.Pojo.Result;
import com.example.caiyu.Service.CommodityManagerService;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
public class CommodityManagerController {
    @Autowired
    private CommodityManagerService commodityManagerService;
    /*
    请帮我生成一段javaweb代码，描述如下：Post请求，请求地址为  /api/commodity/publish，
    请求体为json格式，请求体参数为：name,price,category,images
    存入到Commodity类中，自动生成一个唯一的commodityId，但要遵循以下格式“C-xxxx”，xxxx表从1开始自增
    score初始置为5，commodityStatus置为0，commentsArray置为空数组
    将商品信息存入数据库的Commodity表中，并返回一个Result对象，结果为success
    * */
    public Integer getCommodityCount()
    {
        int count = commodityManagerService.getCommodityCount();
        return count+1;
    }

    @PostMapping("/api/commodity/publish")
    public Result publishCommodity(String name, Float price, String category, String images){
        Commodity commodity = new Commodity(null, name, price, category, images, null, null, null);
        commodity.setCommodityId("C-" + getCommodityCount().toString());
        commodity.setCommodityStatus(0);
        commodity.setScore(5);
        commodity.setCommentsArray(new ArrayList<>());
        commodityManagerService.publishCommodity(commodity);
        return Result.success("商品添加成功");
    }

}
